# TestLevel.gd - 测试关卡脚本
extends Node2D

@onready var player = $Player
@onready var energy_bar = $UI/EnergyBar
@onready var state_label = $UI/StateLabel
@onready var camera = $Camera2D
@onready var shadow_vision_effect = $ShadowVisionLayer/ShadowVisionEffect

func _ready():
	# 连接玩家信号
	if player:
		player.energy_changed.connect(_on_player_energy_changed)
		player.state_changed.connect(_on_player_state_changed)
		player.shadow_vision_toggled.connect(_on_shadow_vision_toggled)
	
	# 初始化影视效果
	setup_shadow_vision_effect()
	
	print("测试关卡已加载 - 主角动画系统测试")

func _process(_delta):
	# 让摄像机跟随玩家
	if player:
		camera.position = player.position

func _on_player_energy_changed(current_energy: float, max_energy: float):
	# 更新能量条
	energy_bar.value = current_energy
	energy_bar.max_value = max_energy

func _on_player_state_changed(new_state: String):
	# 更新状态显示
	state_label.text = "状态: " + new_state

# 设置影视效果
func setup_shadow_vision_effect():
	# 创建shader材质
	var shader_material = ShaderMaterial.new()
	var shader = load("res://shaders/shadow_vision.gdshader")
	shader_material.shader = shader
	
	# 设置shader参数 - 默认状态为黑暗（is_active = false）
	shader_material.set_shader_parameter("is_active", false)
	shader_material.set_shader_parameter("intensity", 0.8)
	shader_material.set_shader_parameter("contrast", 2.0)
	shader_material.set_shader_parameter("tint_color", Color(0.2, 0.2, 0.8, 1))
	
	# 应用材质到影视效果节点
	shadow_vision_effect.material = shader_material

func _on_shadow_vision_toggled(is_active: bool):
	print("TestLevel: _on_shadow_vision_toggled called with is_active: ", is_active)
	# 处理影视能力切换
	if shadow_vision_effect and shadow_vision_effect.material:
		shadow_vision_effect.material.set_shader_parameter("is_active", is_active)
		print("TestLevel: Shader parameter is_active set to ", is_active)
	else:
		print("TestLevel: shadow_vision_effect or its material is null!")
	
	if is_active:
		print("影视能力已激活")
	else:
		print("影视能力已关闭")

func _input(event):
	# 测试用快捷键
	if event.is_action_pressed("ui_cancel"):
		# ESC键返回主菜单
		get_tree().change_scene_to_file("res://scenes/ui/MainMenu.tscn")
